/*
 *
 * Copyright (c) 2009, dana koch
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 */

package btplay;

import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import javax.microedition.io.*;
import javax.microedition.io.file.*;
import java.io.*;
import java.util.*;

final class FileChooserController
{
   FileChooserController()
   {
   }
   
   public String getPath()
   {
      String initialRoot = "/SDCard/";
      String root;
      
      root = initialRoot;
      FileChooser chooser;
      do
      {
         System.err.println("Starting traversal " + root);
         chooser = new FileChooser(root);
         UiApplication.getUiApplication().pushModalScreen(chooser);
         
         String path_component = chooser.pathComponent();
         
         if(path_component.equals("X")) // this really isn't the right way to do it...
         {
            root = "";
            break;
         }
         
         if(!path_component.equals(".."))
            root = root + path_component;
         else
         {
            root = root.substring(0, 
               root.substring(0, root.length() - 1).lastIndexOf('/') ) + "/";
         }
         
         System.err.println(">>> " + root);
         
      }
      while(root.endsWith("/"));
      
      return root;
   }
}

final class FileChooser extends PopupScreen
{
   String path; 
   
   FileChooser(String root)
   {
      super(new VerticalFieldManager(Manager.VERTICAL_SCROLL | Manager.VERTICAL_SCROLLBAR));
      
      this.pathComponent("");
      FileChooserListField files = new FileChooserListField(this, root);
      
      this.add(files);
   }
   
   protected boolean keyChar(char c, int status, int time)
   {
      if(c == Keypad.KEY_ESCAPE)
      {
         pathComponent("X");
         UiApplication.getUiApplication().popScreen(this);
         return true;
      }
      
      return super.keyChar(c, status, time);
   }
    
   void pathComponent(String path)
   {
      this.path = path;
   }
    
   String pathComponent()
   {
      return path;
   }
}

final class FileChooserListField extends ObjectListField 
{
   FileChooser parent;
   String root;
   
   FileChooserListField(FileChooser parent, String root)
   {
      this.parent = parent;
      this.root = root;
      this.set(this.getListing());
   }
   
   protected boolean trackwheelClick(int status, int time)
   {
      int i = this.getSelectedIndex();
      ListFieldCallback cb = this.getCallback();
      Object o = cb.get(this, i);
      String s = (String) o;
      
      parent.pathComponent(s);
      
      UiApplication.getUiApplication().popScreen(parent);
        
      return true;
   }
   
   Object[] getListing()
   {
      Object[] ret = null;
      
      FileConnection c;
      try 
      {
         c = (FileConnection) Connector.open("file://" + root);
      }
      catch(Exception e)
      {
         System.err.println(e.toString());
         return ret;
      }
      
      Enumeration en;
      try
      {
         en = c.list();
      }
      catch(Exception e)
      {
         System.err.println(e.toString());
         return ret;
      }
      
      int i;
      
      Vector v = new Vector();
      i = 0;
      
      if(!root.equals("/SDCard/"))
         v.addElement((Object) "..");
         
      i++;
      while(en.hasMoreElements())
      {
         v.addElement(en.nextElement());
         i++;
      }
      ret = new Object[v.size()];
      v.copyInto(ret);
      
      return ret;
   }
}

